﻿using LdtJudge.DataEngine.Model.EvidenceDerivation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.OptimizationModule.EvidenceDerivation
{
    /// <summary>
    /// 证据效力对要素的认定
    /// </summary>
    public class EvidentiaryEffectt
    {
        /// <summary>
        /// 认定证据状态
        /// </summary>
        /// <param name="eviFactElementRelations">证据与事实关系列表</param>
        /// <returns></returns>
        public List<EviFactElementRelationModel> AffirmEvidenceState(List<EviFactElementRelationModel> eviFactElementRelations)
        {
            if (eviFactElementRelations == null)
                return null;
            // 去掉证据地位、证据效力、证据效力来源都为空的元素
            var validedRelations = eviFactElementRelations.Where(a => !string.IsNullOrEmpty(a.EvidencePlace) && !string.IsNullOrEmpty(a.EvidenceValidSource) && !string.IsNullOrEmpty(a.EvidenceValidType));
            if (validedRelations == null || validedRelations.Count()== 0)
                return null;
            // 按事实要素ID分组
            var groupsInfo = validedRelations.ToList().GroupBy(a => new { a.FactElementId, a.Xpath },a=>a);
            // 遍历分组信息
            foreach (var info in groupsInfo)
            {
                List<EviFactElementRelationModel> factElementGroup = info.ToList<EviFactElementRelationModel>();
                
                if (factElementGroup.Where(a => a.EvidencePlace == "3").Any())
                {
                    // 存在核心证据的话，获取核心证据
                    List<EviFactElementRelationModel> coreEvidences = factElementGroup.Where(a => a.EvidencePlace == "3").ToList();
                    // 判断核心证据是否都是合格证据
                    if (coreEvidences.Where(a => a.EvidenceValidType != "1" && !string.IsNullOrEmpty(a.EvidenceValidType)).Any())
                    {
                        // 核心证据中存在不是合格证据的证据，将这些证据标记为待认定证据
                        coreEvidences.ForEach(a => a.EvidenceAffirmStatus = "0");
                    }
                    else
                    {
                        // 核心证据中都是合格证据的证据，将这些证据标记为可以认定证据
                        coreEvidences.ForEach(a => a.EvidenceAffirmStatus = "1");
                    }
                }
                else
                {
                    // 没有核心证据,看看这些证据是不是都是一般证据
                    if(factElementGroup.Where(a => a.EvidencePlace != "1" && !string.IsNullOrEmpty(a.EvidencePlace)).Any())
                    {
                        // 不都是一般证据，设置证据状态为待认定
                        factElementGroup.ForEach(a => a.EvidenceAffirmStatus = "0");
                    }
                    else
                    {
                        List<EviFactElementRelationModel> generalEvidences = factElementGroup.Where(a => a.EvidencePlace == "1").ToList();
                        if (generalEvidences.Where(a => a.EvidenceValidType != "1").Any())
                        {
                            // 如果一般证据中存在不合格证据  那么设置待认定
                            generalEvidences.ForEach(a => a.EvidenceAffirmStatus = "0");
                        }
                        else
                        {
                            // 如果一般证据中都是合格证据  那么需要判断合格证据的数量
                            if (generalEvidences.Count() > 1)
                                generalEvidences.ForEach(a => a.EvidenceAffirmStatus = "1");// 一般证据中都是合格证据，且这样的证据数量大于1，那么设定这些证据为可认定证据
                            else
                                generalEvidences.ForEach(a => a.EvidenceAffirmStatus = "0");// 一般证据中都是合格证据，且这样的证据数量只有1个，那么设定这些证据为待认定证据
                        }
                    }
                }
            }
            return eviFactElementRelations;
        }
    }
}
